package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
	"time"
)

func main() {
	header:= make(map[string]string)
	var Signature = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2NjE2MDUyODksImp0aSI6IjM0NXw3MTlhYzg2OGZmYzQyNWI3MmYwZTdmOTMyYThiNWEwYiIsImlzcyI6Il8ifQ.pHWu-6tVyxrPDAlzKng6iAjgmJHm-4YdZI0QLP4k2sA"

	header["Signature"]= Signature

	newTsfFileParams:= NewTsfFileParams{
		Method: "POST",
		Uri: "https://oms.cht-group.net/api/base/inner/get_sku_list_by_sku",
		Header: nil,
		ParamName: "",
		FilePath: "",
	}

	fields:=[]string{
		"category_id","supplier_ext_id","currency","sku"}
	skus:=[]string{"WIMAX700"}

	req:= GetSku{
		Skus: skus,
		WarehouseId: 46,
		Fields: fields,
	}



	res:=postFile(&newTsfFileParams,req)

	newResFile:= NewResFile{}
	err := json.Unmarshal(res, &newResFile)
	if err != nil {
		return
	}
	fmt.Println(newResFile)
	//fmt.Println(newResFile.Data)
	//fmt.Println(res)

}


func postFile(TsfFileParams *NewTsfFileParams,st interface{})([]byte){
	body := &bytes.Buffer{}
	// 带上表单参数内容
	//sss,_:= json.Marshal(st)

	err := json.NewEncoder(body).Encode(st)
	if err != nil {
		return nil
	}
	fmt.Println(body)
	request, err := http.NewRequest(TsfFileParams.Method, TsfFileParams.Uri, body)
	request.Header.Set("Content-Type", "application/json")

	t := http.DefaultTransport.(*http.Transport).Clone()
	t.MaxIdleConns = 100
	t.MaxConnsPerHost = 100
	t.MaxIdleConnsPerHost = 100

	clt := http.Client{
		Timeout:   10 * time.Second,
		Transport: t,
	}
	defer clt.CloseIdleConnections()
	res, err := clt.Do(request)
	fmt.Println(res)
	//return nil
	//http返回的response的body必须close,否则就会有内存泄露
	defer func() {
		err := res.Body.Close()
		if err != nil {
			fmt.Println(" res body close err =",err)
		}
		fmt.Println(" finish ")
	}()
	if err != nil {
		fmt.Println(" transfer url_file err =", err)
	}
	responseBody, err1 := ioutil.ReadAll(res.Body)
	if err != nil {
		fmt.Println(" ioUtil.ReadAll  err =", err1)
	}
	return responseBody
}

type NewTsfFileParams struct {
	Method		string				`json:"method"`
	Uri			string				`json:"uri"`
	Header 		map[string]string	`json:"header"`
	ParamName 	string				`json:"param_name"`
	FilePath 	string				`json:"file_path"`
}

type GetSku struct {
	Skus []string `json:"skus"`
	WarehouseId int `json:"warehouse_id"`
	Fields []string	`json:"fields"`
}

type NewResFile struct {
	Status string `json:"status"`
	Data   []skuData `json:"data"`
	Code int `json:"code"`
}

type skuData struct {
	CategoryId int `json:"category_id"`
	SupplierExtId int `json:"supplier_ext_id"`
	Currency string `json:"currency"`
	Sku string `json:"sku"`
	ContractCategoryId int `json:"contract_category_id"`
	IsMrp int `json:"is_mrp"`
	SupplierSn string `json:"supplier_sn"`
	Category categoryData `json:"category"`
	SupplierExt supplierExtData `json:"supplier_ext"`
}

type categoryData struct {
	Id int `json:"id"`
	ParentId int `json:"parent_id"`
	Name string `json:"name"`
	Sort int `json:"sort"`
}

type supplierExtData struct {
	Id int `json:"id"`
	SupplierId int `json:"supplier_id"`
	SupplierCode string `json:"supplier_code"`
	TaxType int `json:"tax_type"`
	Supplier supplierData `json:"supplier"`
}

type supplierData struct {
	Id int `json:"id"`
	SupplierNo string `json:"supplier_no"`
	Name string `json:"name"`
}